<?php
class user extends basic implements remote_export {
	public	$server = null,
			$host = '',
			$ip = '',
			$username = '',
			$gekos = '',
			$fakehost = array(), //histry, last = active
			$signon = 0,
			$my_signon = 0,
			$stamp = 0,
			$modes = null,
			$swhois = '',
			$flags = array(),
			$tags = array(),
			$channels = array(), //index=oid
			$lang = '',
			$timeshift = 0,
			$away = '',
			$away_stamp = 0;
	public function __sleep() {
		foreach ($this as $key => &$val) {
			if ($val instanceof remote_export) {
				$this->static[$key] = new basic_placeholder($val->oid());
			}
		}
		$this->static['channels'] = array();
		foreach ($this->channels as $key => &$chan) {
			$this->static['channels'][$key] = new basic_placeholder($chan->oid());
		}
		$ret = array('name','oid','static','host','ip','username','gekos','fakehost','signon','my_signon',
		'stamp','modes','swhois','flags','tags','lang','timeshift','away','away_stamp');
		foreach (array('ngi','ni') as $obj) {
			if (is_null($this->$obj))
				$ret[] = $obj;
		}
		return $ret;
	}
	public function __wakeup() {
		global $users;
		if (!is_array($users))
			throw new Exception("invalid type of global \$users");
		$users[$this->tolower()] =& $this;
		foreach ($this->static as $key => $static) {
			if ($static instanceof remote_placeholder)
				$this->$key = $static;
			elseif (is_array($static)) {
				$this->$key = array();
				foreach ($static as $skey => $sval) {
					if (!$sval instanceof remote_placeholder) continue;
					$this->$key[$skey] = $sval;
				}
			}
		}
	}
	public function construct(array $pars = null) {
		extract($pars, EXTR_REFS);
		if (!isset($server) or !$server instanceof server)
			throw new Exepction("invalid server parameter specified");
		if (array_key_exists($this->tolower(),$GLOBALS['users']))
			throw new Exception("duplicate user $this->name in user list",016);
		$this->server =& $server;
		$this->server->users[$this->oid()] =& $this;
		$this->my_signon = time();
		$this->modes = new modes_user($this);
		$GLOBALS['users'][$this->tolower()] =& $this;
	}
	public function destruct ($reason = '') {
		try {
			callback_module::run('user_signoff',array(&$this,$reason));
		} catch (Exception $ex) {
			return $ex->getMessage();
		}
		unset($GLOBALS['users'][$this->tolower()]);
		unset($GLOBALS['basics'][$this->oid()]);
		unset($this->server->users[$this->oid()]);
		foreach ($this->channels as &$channel)
			$channel->quit($this, $reason);
	}
	public function valid_name($name) {
		return preg_match('/^[A-}][A-}0-9\-]{0,31}$/i',$name);
	}
	public function nick_change($newnick) {
		if (!abs(strcasecmp($this->name, $newnick))) {
			$this->name = $newnick;
			return true;
		}
		if (isset($GLOBALS['users'][strtolower($newnick)]))
			return false;
		$GLOBALS['users'][strtolower($newnick)] =& $this;
		unset($GLOBALS['users'][$this->tolower()]);
		$this->name = $newnick;
		return true;
	}
	function lnick () {
		return strtolower($this->name);
	}
	public function mode (basic &$source, $apply) {
		try {
			callback_module::run('user_mode',array(&$this,&$source,$apply));
		} catch (Exception $ex) {
			return $ex->getMessage();
		}
		modes_apply($this->modes, $apply);
	}
	public function is_identified () {
		return false;
	}
}
?>
